package com.wjh.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wenjianhai
 * @date 2023/6/27
 * @since JDK 17
 */
@Slf4j
@Configuration
public class RedissonConfig {
	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.port}")
	private int port;
	@Value("${spring.redis.password}")
	private String password;
	@Value("${spring.redis.database}")
	private int database;

	@Bean(destroyMethod = "shutdown")
	public RedissonClient redissonClient() {
        log.info("Create RedissonClient - start");
		Config config = new Config();

		if (StringUtils.isNotBlank(password)) {
			config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password).setDatabase(database);
		} else {
			config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
		}
		return Redisson.create(config);
	}
}
